<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Vue 实现简易的留言板</title>
    <script src="../js/vue.js"></script>
</head>

<body>
    <div id="app">
        <h2>title ={{title}}</h2>
        <div>
            <h4>循环留言</h4>
            <div style="width: 70%;" v-if="list.length>0">
                <div v-for="(item,index) in list" :key="index"
                    style="margin-bottom: 5px; border-bottom: 1px solid #ccc;">
                    <p>序号:{{index +1 }}</p>
                    <h3>
                        {{item.content}}
                    </h3>
                    <p>
                        <button @click="list.splice(index,1)">删除</button>
                        <button @click="updateItem(item,index)">修改</button>
                    </p>
                </div>
            </div>
            <div v-else>
                <h2>暂无留言数据</h2>
                <hr>
            </div>
            <h4>添加留言</h4>
            <div style="width: 70%;">
            <div>
                <p>
                    <input type="text" v-model="content">
                </p>
                <button @click="addToComment">提交留言</button>
            </div>
            </div>
        </div>
    </div>
</body>
<script>
    const vm = new Vue({
        el: "#app",
        data: {
            title: 'Vue 案例 留言板',
            list: [
                {
                    content: '新年快乐'
                }
            ],
            content: '1111'
        },
        methods:{
            addToComment(){
                this.list.push({
                    content:this.content
                })
                this.content='';
            },
            updateItem(item,index){
                let content = window.prompt(index,item.content)
                console.log(content);
                if(content&&content!=''){
                    item.content = content;
                    this.list.splice(index,1,item)  // 替换 修改后的数据
                }
            }
        }
    })
</script>

</html>